package main

import (
	"fmt"
	_ "github.com/Shopify/sarama"
	"github.com/go-ini/ini"
	"github.com/sirupsen/logrus"
	"logAgent/etcd"
	"logAgent/kafkafile"
	"logAgent/tailfile"
)

type Config struct {
	KafkaConfig `ini:"kafka"`
	EtcdConfig `ini:"etcd"`
}

type KafkaConfig struct {
	Address string `ini:"address"`
	ChanSize int `ini:"chan_size"`
}

type EtcdConfig struct {
	Address string `ini:"address"`
	CollectKey string `ini:"collect_key"`
}

// 阿里云ip:			120.79.89.189

func run() {
	select{

	}
}


func main() {
	var configObj = new(Config)
	//读取ini配置文件
	err := ini.MapTo(configObj, "./config/config.ini")
	if err != nil {
		logrus.Error("load config failed err is: %v", err)
		return
	}
	fmt.Println("the configObj is: ", configObj)

	//初始化链接kafka
	err = kafkafile.InitKafka([]string{configObj.KafkaConfig.Address}, configObj.KafkaConfig.ChanSize)
	if err != nil {
		logrus.Error("init kafka failed err:%v", err)
		return
	}

	//初始化etcd
	err = etcd.InitEtcd([]string{configObj.EtcdConfig.Address})
	if err != nil {
		logrus.Info("连接etcd失败！")
		return
	}
	etcd.PutValue()

	//从etcd中拉去要收集的日志项配置
	allConfig, err := etcd.EtcdGetConf(configObj.EtcdConfig.CollectKey)
	if err != nil {
		logrus.Info("根据key获取value失败！")
		return
	}
	//启一个进程去监控etcd中key值对应value值的变化
	go etcd.EtcdWatch(configObj.EtcdConfig.CollectKey)

	//根据配置中的路径初始化tail
	err = tailfile.Init(allConfig)  //把日志路径配置信息发送给init
	if err != nil {
		logrus.Info("收集日志信息失败！")
		return
	}
	run()
}
